package com.sinochem.es.demo.controller;

import com.alibaba.fastjson.JSON;
import com.sinochem.es.demo.entity.RmPolicyPublication;
import com.sinochem.es.demo.service.IRmPolicyPublicationService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : Barry.Yu
 * @date : 2022/2/8 16:37
 */
@RestController
@RequestMapping("/policy")
public class RmPolicyPublicationController {


    @Resource
    private IRmPolicyPublicationService rmPolicyPublicationService;

    @RequestMapping("/save")
    public String saveToEs(){
        List<RmPolicyPublication> list = rmPolicyPublicationService.list();
        List<RmPolicyPublication> collect = list.stream().map(rmPolicyPublication -> {rmPolicyPublication.setId(null);
        return rmPolicyPublication;
        }).collect(Collectors.toList());
        rmPolicyPublicationService.saveEs(collect);
        return "OK";
    }

    @RequestMapping("/query")
    public String HighQuery(){
        List<RmPolicyPublication> rmPolicyPublications = rmPolicyPublicationService.highQuery();
        return JSON.toJSONString(rmPolicyPublications);
    }

    @RequestMapping("/update")
    public String updateById(@RequestParam String id){
        RmPolicyPublication rmPolicyPublication = rmPolicyPublicationService.findById(id);
        rmPolicyPublication.setText("我要修改一下试试");
        rmPolicyPublicationService.saveByEs(rmPolicyPublication);
        return "ok";
    }






}
